package com.sinopec.basemodule.dialog

import android.os.Bundle
import android.text.Html
import android.view.View
import android.widget.TextView
import com.sinopec.basemodule.R
import com.sinopec.basemodule.utils.kotlin.isEqualsVisibility
import com.sinopec.basemodule.utils.kotlin.setNewVisibility
import kotlinx.android.synthetic.main.bm_df_hint.*

/**
 * 类描述：提示框<br/>
 * 创建人：lwj<br/>
 * 创建时间：2019/7/22 10:13 <br/>
 */
class DFHint : DFBase() {
    interface Callback {
        fun clickRight() = Unit
        fun clickLeft() = Unit
        fun dismiss() = Unit
    }

    /**
     * 标题
     */
    private var mTitleStr: String? = null

    /**
     * 内容
     */
    private var mContentStr: String? = null

    /**
     * 左边按钮内容
     */
    private var mLeftStr: String? = null

    /**
     * 右边按钮内容
     */
    private var mRightStr: String? = null

    companion object {
        @JvmStatic
        @JvmOverloads
        fun newInstance(
            title: String? = "标题",//空 或者 null 不显示
            content: String? = "内容",//空 或者 null 不显示
            leftText: String? = "",//空 或者 null 不显示
            rightText: String? = ""//空 或者 null 不显示
        ): DFHint {
            val dfHint = DFHint()
            val bundle = Bundle()
            bundle.putString("title", title)
            bundle.putString("content", content)
            if (!leftText.isNullOrEmpty()) {
                bundle.putString("left", leftText)
            }
            if (!rightText.isNullOrEmpty()) {
                bundle.putString("right", rightText)
            }
            dfHint.arguments = bundle
            return dfHint
        }
    }

    private var mCallback: Callback? = null

    fun setCallback(callback: Callback?) {
        if (callback == null) {
            return
        }
        mCallback = callback
    }

    override fun createView(): Int {
        initBundle()
        //外部点击事件setCancelable(true);//getDialog().setCanceledOnTouchOutside(false);
        //左右按键都没有，可以点击外部取消
        //左右按键有，就不可以点击外部取消
        isCancelable = (mLeftStr.isNullOrEmpty()
                && mRightStr.isNullOrEmpty())
        return R.layout.bm_df_hint
    }

    override fun onDetach() {
        super.onDetach()
        if (mCallback != null) {
            if (isCancelable) {
                //如果可以点击外部取消，表示没有下面两个按钮，需要回调消失事件
                mCallback?.dismiss()
            }
            mCallback = null
        }
    }

    private fun initBundle() {
        val bundle = arguments ?: throw IllegalArgumentException("请根据newInstance传递参数传递")
        if (bundle.containsKey("title")) {
            mTitleStr = bundle.getString("title")
        }
        if (bundle.containsKey("content")) {
            mContentStr = bundle.getString("content")
        }
        if (bundle.containsKey("left")) {
            mLeftStr = bundle.getString("left")
        }
        if (bundle.containsKey("right")) {
            mRightStr = bundle.getString("right")
        }
    }

    override fun initView(view: View?) {
        //1.判断是否有标题
        setUiVisibleOrGone(mTitleStr, mTitleTv)
        //2.判断是否有内容
        setUiVisibleOrGone(mContentStr, mContentTv)
        //3.判断左边按钮是否显示
        setUiVisibleOrGone(mLeftStr, mLeftTv)
        //4.判断右边按钮是否显示
        setUiVisibleOrGone(mRightStr, mRightTv)

        //设置点击事件
        if (mLeftTv.isEqualsVisibility(View.VISIBLE)) {
            mLeftTv.setOnClickListener {
                dismiss()
                mCallback?.clickLeft()
            }
        }

        if (mRightTv.isEqualsVisibility(View.VISIBLE)) {
            mRightTv.setOnClickListener {
                dismiss()
                mCallback?.clickRight()
            }
        }
    }

    /**
     * 设置ui展示
     */
    private fun setUiVisibleOrGone(str: String?, tv: TextView) {
        if (!str.isNullOrEmpty()) {
            tv.setNewVisibility(View.VISIBLE)
            tv.text = Html.fromHtml(str, imageGetter, null)
        } else {
            tv.setNewVisibility(View.GONE)
        }
    }

    private val imageGetter = Html.ImageGetter { source ->
        val id = source.toInt()
        val d = resources.getDrawable(id)
        d.setBounds(0, 0, d.intrinsicWidth, d.intrinsicHeight)
        d
    }
}


fun dfHint(
    content: String = "",
    title: String = "提示",
    leftText: String = "取消",
    rightText: String = "确认",
    whenResponse: (isRight: Boolean) -> Unit
): DFHint {
    val dfHint = DFHint.newInstance(title, content, leftText, rightText)
    dfHint.setCallback(object : DFHint.Callback {
        override fun dismiss() {}
        override fun clickLeft() {
            whenResponse(false)
        }

        override fun clickRight() {
            whenResponse(true)
        }
    })
    return dfHint
}